import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ProvinceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        try {
            Connection con = DruidPool.getCon();
            Statement statement = con.createStatement();
            String sql = "select * from province";
            ResultSet rs = statement.executeQuery(sql);
            List<ProvinceBean> provinces = new ArrayList();
            while(rs.next()) {
                ProvinceBean pb = new ProvinceBean();
                pb.setId(rs.getInt("id"));
                pb.setPname(rs.getString("pname"));
                provinces.add(pb);
            }
            String result = JSON.toJSONString(provinces);
            resp.getWriter().write(result);
            DruidPool.close(rs,statement,null,con);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
